package com.hm.health.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.goodix.http.jsonparams.request.AddSubjectParams;
import com.hm.health.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liwenlin on 16-11-9.
 */

public class AddSubjectDialog extends AlertDialog implements View.OnClickListener{
    private static final String TAG = "AddSubjectDialog";

    private static final int MSG_TYPE_LOAD_SKIN_INFO_FINISH = 1;
    private Context mContext;
    private ClickListenerInterface mClickListenerInterface;
    String mSex;
    String mSkin;
    EditText mSubjectNumberText;
    EditText mSubjectNameText;
    EditText mSubjectAgeText;
    EditText mSubjectHeightText;
    EditText mSubjectWeightText;
    Spinner mSubjectSexSpinner;
    Spinner mSubjectSkinSpinner;
    EditText mSubjectRsyText;
    EditText mSubjectMaxVoText;
    EditText mSubjectMaxHrText;
    EditText mSubjectRestingHrText;

    ArrayAdapter<String> mSubjectSkinSpinnerAdapter;
    List<String> mSubjectskinDataArray = new ArrayList<String>();
    List<Integer> mSubjectskinIDArray = new ArrayList<Integer>();
    String mSelectedSubjectSkin;

    public interface ClickListenerInterface {
        void doConfirm(AddSubjectParams params);
        void doCancel();
    }

    public AddSubjectDialog(Context context) {
        super(context, R.style.MyDialog);
        mContext = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initSkinData();
        mSex = getContext().getString(R.string.male);
    }

    private void initSkinData() {
//        Log.i(TAG, "fillDataFromDB");
//        DbProvider.getInstance(mContext).open();
//        Cursor skinCursor = DbProvider.getInstance(mContext).getAllSkinInfo();
//        int skinNameColumnIndex = skinCursor.getColumnIndex(DbProvider.SKININFO_SCENE_NAME_COLUMN);
//        int skinIDColumnIndex = skinCursor.getColumnIndex(DbProvider.SKININFO_SCENE_ID_COLUMN);
//        while (skinCursor.moveToNext()) {
//            mSubjectskinDataArray.add(skinCursor.getString(skinNameColumnIndex));
//            mSubjectskinIDArray.add(skinCursor.getInt(skinIDColumnIndex));
//        }
//        skinCursor.close();
//        DbProvider.getInstance(mContext).close();
        mSubjectskinDataArray.add(mContext.getString(R.string.skin_yellow));
        mSubjectskinDataArray.add(mContext.getString(R.string.skin_white));
        mSubjectskinDataArray.add(mContext.getString(R.string.skin_brown));
        mSubjectskinDataArray.add(mContext.getString(R.string.skin_black));
        mSubjectskinDataArray.add(mContext.getString(R.string.skin_blue));
        mHandler.sendEmptyMessage(MSG_TYPE_LOAD_SKIN_INFO_FINISH);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_TYPE_LOAD_SKIN_INFO_FINISH:
                    initSkinSpinner();
                    break;
                default:
                    break;
            }
        }
    };

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.layout_add_subject_dialog, null);
        setContentView(view);
        mSubjectNumberText = view.findViewById(R.id.subject_num_text);
        mSubjectNameText = view.findViewById(R.id.subject_name_text);
        mSubjectAgeText = view.findViewById(R.id.subject_age_text);
        mSubjectHeightText = view.findViewById(R.id.subject_height_text);
        mSubjectWeightText = view.findViewById(R.id.subject_weight_text);
        mSubjectSexSpinner = view.findViewById(R.id.subject_sex_spinner);
        mSubjectSkinSpinner = view.findViewById(R.id.subject_skin_spinner);
        mSubjectRsyText = view.findViewById(R.id.subject_rsy_text);
        mSubjectMaxVoText = view.findViewById(R.id.subject_max_vo_text);
        mSubjectMaxHrText = view.findViewById(R.id.subject_max_hr_text);
        mSubjectRestingHrText = view.findViewById(R.id.subject_resting_hr_text);
        view.findViewById(R.id.scene_confirm).setOnClickListener(this);
        view.findViewById(R.id.scene_cancel).setOnClickListener(this);
        mSubjectSexSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.i(TAG, "onItemSelected");
                String[] sex = mContext.getResources().getStringArray(R.array.sex);
                mSex = sex[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.i(TAG, "onNothingSelected");
            }
        });
        WindowManager windowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        Display display  = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        Rect rect = new Rect();
        display.getRectSize(rect);
        layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        layoutParams.width = (int)(rect.width() * 0.8);
        layoutParams.height = (int)(rect.height() * 0.8);

    }

    private void initSkinSpinner() {
        mSubjectSkinSpinnerAdapter = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_spinner_item, mSubjectskinDataArray);
        mSubjectSkinSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSubjectSkinSpinner.setAdapter(mSubjectSkinSpinnerAdapter);
        mSubjectSkinSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.i(TAG, "subject onItemSelected");
                mSelectedSubjectSkin = mSubjectskinDataArray.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.i(TAG, "subject onNothingSelected");
            }
        });
    }

    @Override
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.scene_confirm:
                String name = mSubjectNameText.getText().toString();
                String number = mSubjectNumberText.getText().toString();
                byte age = -1;
                short height = -1;
                short weight = -1;
                int rsy = -1;
                int maxVo = -1;
                int maxHr = -1;
                int restingHr = -1;
                try {
                    age = Byte.valueOf(mSubjectAgeText.getText().toString());
                    height = Short.valueOf(mSubjectHeightText.getText().toString());
                    weight = Short.valueOf(mSubjectWeightText.getText().toString());
                    rsy = Integer.valueOf(mSubjectRsyText.getText().toString());
                    maxVo = Integer.valueOf(mSubjectMaxVoText.getText().toString());
                    maxHr = Integer.valueOf(mSubjectMaxHrText.getText().toString());
                    restingHr = Integer.valueOf(mSubjectRestingHrText.getText().toString());
                } catch(NumberFormatException e) {
                    e.printStackTrace();
                }
                if (name == null || name.length() <= 0 || age <= 0 || mSex == null || mSelectedSubjectSkin == null) {
                    Toast.makeText(mContext, mContext.getString(R.string.scene_info_error_hint), Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.d(TAG, "name:" + name + "; num:" + number + "; age:" + age + "; sex:"
                        + mSex + "; height:" + height + ", weight:" + weight + "; rsy:" + rsy +
                        "; mSelectedSubjectSkin:" + mSelectedSubjectSkin + "; maxvo:" + maxVo
                        + "; maxHr:" + maxHr +"; restHr:" + restingHr);
                AddSubjectParams params = new AddSubjectParams();
                params.setSubjectName(name);
                params.setSubjectGender(mSex);
                params.setSubjectAge(age);
                params.setSubjectSkin(mSelectedSubjectSkin);
                if (number != null && number.length() > 0) {
                    params.setSubjectNum(number);
                }
                if (height > 0) {
                    params.setSubjectHeight(height);
                }
                if (weight > 0) {
                    params.setSubjectWeight(weight);
                }
                if (rsy > 0) {
                    params.setSubjectRsy(rsy);
                }
                if (maxVo > 0) {
                    params.setSubjectMaxVo(maxVo);
                }
                if (maxHr > 0) {
                    params.setSubjectMaxHr(maxHr);
                }
                if (restingHr > 0) {
                    params.setSubjectRestingHr(restingHr);
                }
                mClickListenerInterface.doConfirm(params);
                break;
            case R.id.scene_cancel:
                mClickListenerInterface.doCancel();
                break;
            default:
                break;
        }
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        mClickListenerInterface = clickListenerInterface;
    }
}
